package com.fivefu.core.skyeye.event.services.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.base.common.utils.date.DateUtils;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.skyeye.event.entity.DbAiEvent;
import com.fivefu.core.skyeye.event.entity.DbAiRedisStream;
import com.fivefu.core.skyeye.event.entity.DbAiScene;
import com.fivefu.core.skyeye.event.mapper.DbAiRedisStreamMapper;
import com.fivefu.core.skyeye.event.services.DbAiEventImageService;
import com.fivefu.core.skyeye.event.services.DbAiEventService;
import com.fivefu.core.skyeye.event.services.DbAiRedisStreamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fivefu.core.skyeye.event.services.DbAiSceneService;
import com.fivefu.core.skyeye.event.vo.EventImageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * redis队列消息读取 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-08-16 09:48:40
 */
@Service
public class DbAiRedisStreamServiceImpl extends ServiceImpl<DbAiRedisStreamMapper, DbAiRedisStream> implements DbAiRedisStreamService {

    /**
     * 通过记录编号查询消息
     * @param recordID
     * @return
     */
    @Override
    public DbAiRedisStream queryRedisStreamByRecordID(String recordID){
        QueryWrapper<DbAiRedisStream> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("record_id",recordID);
        DbAiRedisStream stream = baseMapper.selectOne(wrapper);
        return stream;
    }

    /**
     * 新增消息记录
     * @param recordID
     * @param streamName
     * @param streamValue
     */
    @Override
    public Long insertRedisStream(String recordID, String streamName, String streamValue){
        LocalDateTime time = LocalDateTime.now();
        DbAiRedisStream redisStream = new DbAiRedisStream();
        redisStream.setCreatedTime(time);
        redisStream.setUpdatedTime(time);
        redisStream.setIsDelete(0);
        redisStream.setRecordId(recordID);
        redisStream.setStreamName(streamName);
        redisStream.setStreamValue(streamValue);
        baseMapper.insert(redisStream);
        return redisStream.getId();
    }

    /**
     * 编辑消息记录
     * @param id 主键编号
     * @param type 类型
     * @param cvId 场景编号
     * @param eventId 案卷编号
     */
    @Override
    public void updateRedisStream(Long id, String type, Integer cvId, Long eventId){
        DbAiRedisStream redisStream = new DbAiRedisStream();
        redisStream.setId(id);
        redisStream.setUpdatedTime(LocalDateTime.now());
        redisStream.setMqType(type);
        redisStream.setCvId(cvId);
        redisStream.setEventId(eventId);
        baseMapper.updateById(redisStream);
    }

    /**
     * 变更消息记录状态
     * @param id 主键编号
     * @param status 是否成功(0否1是)
     */
    @Override
    public void updateRedisStreamStatus(Long id, Integer status){
        DbAiRedisStream redisStream = new DbAiRedisStream();
        redisStream.setId(id);
        redisStream.setStatus(status);
        redisStream.setUpdatedTime(LocalDateTime.now());
        baseMapper.updateById(redisStream);
    }

    /**
     * 通过状态查询消息集合
     * @param status 是否成功(0否1是)
     * @return
     */
    @Override
    public List<DbAiRedisStream> queryRedisStreamListByStatus(Integer status){
        QueryWrapper<DbAiRedisStream> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (status != null) {
            wrapper.eq("status", status);
        }
        List<DbAiRedisStream> list = baseMapper.selectList(wrapper);
        return list;
    }

}
